package com.situ.student.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
 * Filter过滤器，处理所有Servlet的post的编码问题
 * @author 18234
 *
 */
public class EncodingFilter implements Filter {
	//构造方法
	public EncodingFilter() {
		
	}
	//初始化
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}
	//相当于service方法，
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
			HttpServletRequest httpServletRequest = (HttpServletRequest) request;
			System.out.println("EncodingFilter.doFilter()");
			//处理编码 表单里面method="get" method="post"
			String method = httpServletRequest.getMethod();//method方法只能httpservlet才能用
			if (method.equalsIgnoreCase("post")) {
				request.setCharacterEncoding("UTF-8");
			}
			//下一个过滤器或放行
			chain.doFilter(request, response);
	}
	//销毁
	@Override
	public void destroy() {
		
	}

}
